package cn.itcast.collection

import scala.collection.mutable
import scala.collection.mutable.ArrayBuffer

/**
  * @Package cn.itcast.collection
  * @author KTL
  * @date 2021/5/15 0015 15:23
  * @version V1.0
  * @Copyright © 2015-04-29  One for each, and two for each
  *           Desc 演示scala中Array中基本使用-
  */
object Demo01_Seq_Array {
  def main(args: Array[String]): Unit = {
    /**
      *不可变/数组
      */
    val  arr1 =new Array[Int](5)
    val arr2 =Array(1,2,3,4,5)
    println(arr1.length)
    println(arr1)
    println(arr1.toBuffer)
    println(arr1.mkString("|"))


    println(arr2)
    println(arr2.length)
    println(arr2.toBuffer)
    println(arr2.mkString("|"))

    arr1(0) =100
    println(arr1.toBuffer)


    /**
      * 可变/变长数组
      * 需要导入 import scala.collection.mutable.ArrayBuffer包
      * val/var 变量名 =ArrayBuffer[T]()
      * val/var 变量名 =ArrayBuffer(元素1、元素2、元素3...)
      */
    var arry3: ArrayBuffer[Int] =ArrayBuffer[Int]()
    var arry4: ArrayBuffer[Int] =ArrayBuffer(1,2)
    arry3 += 1


    //不可变转可变:返回一个可变的  toBuffer
    val result: mutable.Buffer[Int] = arr1.toBuffer
    //arr1 原来的还是不可变得

    //可变转化为不可变:返回一个不可变的
    val result2: Array[Int] = arry3.toArray
    arry3+=1    // 原来的还是可变的


}
}
